// 格式化时间
function formatTime(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

// 格式化日期
function formatDate(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return `${year}-${month}-${day}`
}

// 获取当前时间戳
function getCurrentTimestamp() {
  return Date.now()
}

// 生成唯一ID
function generateId() {
  return Math.random().toString(36).substr(2, 9)
}

// 防抖函数
function debounce(func, wait) {
  let timeout
  return function () {
    const context = this
    const args = arguments
    clearTimeout(timeout)
    timeout = setTimeout(() => func.apply(context, args), wait)
  }
}

// 节流函数
function throttle(func, limit) {
  let inThrottle
  return function () {
    const args = arguments
    const context = this
    if (!inThrottle) {
      func.apply(context, args)
      inThrottle = true
      setTimeout(() => inThrottle = false, limit)
    }
  }
}

// 深拷贝对象
function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') return obj
  if (obj instanceof Date) return new Date(obj)
  if (obj instanceof Array) return obj.map(item => deepClone(item))
  if (obj instanceof Object) {
    const clonedObj = {}
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        clonedObj[key] = deepClone(obj[key])
      }
    }
    return clonedObj
  }
}

// 数组去重
function uniqueArray(arr) {
  return [...new Set(arr)]
}

// 检查是否为空对象
function isEmptyObject(obj) {
  return Object.keys(obj).length === 0
}

module.exports = {
  formatTime,
  formatDate,
  getCurrentTimestamp,
  generateId,
  debounce,
  throttle,
  deepClone,
  uniqueArray,
  isEmptyObject
}